package com.liddhome.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.liddhome.entity.Message;
import com.liddhome.entity.PageBean;
import com.liddhome.service.MessageService;
import com.liddhome.util.DateUtil;
import com.liddhome.util.PageUtil;
import com.liddhome.util.ResponseUtil;
import com.liddhome.util.StringUtil;

import net.sf.json.JSONObject;

/**
 * 留言Controller层
 * @author lidd
 *
 */
@Controller
@RequestMapping("/message")
public class MessageController {
	
	@Resource
	private MessageService messageService;
	
	@RequestMapping("")
	public ModelAndView index(HttpServletRequest request,HttpSession session)throws Exception{
		String nickName=(String) session.getAttribute("nickName");
		String email=(String) session.getAttribute("email");
		ModelAndView mav=new ModelAndView();
		mav.addObject("nickName",nickName);
		mav.addObject("email",email);
		mav.addObject("mainPage", "foreground/blog/message.jsp");
		mav.addObject("pageTitle","LiddBlog");
		mav.setViewName("mainTemp");
		return mav;
	}
	
	@RequestMapping("/loadMessages")
	public String loadMessages(@RequestParam(value="page",required=false)String page,HttpServletRequest request,HttpServletResponse response)throws Exception{
		JSONObject result=new JSONObject();
		if(StringUtil.isEmpty(page)){
			page="1";
		}
		PageBean pageBean=new PageBean(Integer.parseInt(page),20);
		Integer[] stateArray = new Integer[] {0,1};
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("start", pageBean.getStart());
		map.put("size", pageBean.getPageSize());
		map.put("stateArray", stateArray);
		List<Message> messageList=messageService.list(map);
		result.put("success", true);
		result.put("totalMessage", messageService.getTotal(map));
		result.put("messageList", messageList);
		StringBuffer param=new StringBuffer(); // 查询参数
		result.put("pageCode",PageUtil.messagePagination(messageService.getTotal(map), Integer.parseInt(page), 20));
		ResponseUtil.write(response, result);
		return null;
	}
	
	@RequestMapping("reply")
	public String getReplies(@RequestParam(value="id",required=false)String id,HttpServletRequest request,HttpServletResponse response)throws Exception{
		JSONObject result=new JSONObject();
		List<Message> replyList=messageService.findByParent(id);
		result.put("messageList", replyList);
		ResponseUtil.write(response, result);
		return null;
	}
	
	/**
	 * 添加或者修改留言
	 * @param comment
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/save")
	public String save(Message message,@RequestParam("imageCode") String imageCode,HttpServletRequest request,HttpServletResponse response,HttpSession session)throws Exception{
		String sRand=(String) session.getAttribute("sRand"); // 获取系统生成的验证码
		String sNickName=(String)session.getAttribute("nickName");
		String nickName=message.getNickName();
		String email=message.getEmail();
		JSONObject result=new JSONObject();
		int resultTotal=0; // 操作的记录条数
		if(!imageCode.equals(sRand)){
			result.put("success", false);
			result.put("errorInfo", "验证码填写错误！");
		}else if(StringUtil.isEmpty(nickName)){
			result.put("success", false);
			result.put("errorInfo", "请填写您的昵称！");
		}else if(StringUtil.isEmpty(message.getContent())) {
			result.put("success", false);
			result.put("errorInfo", "您未填写留言内容！");
		}else{
			String userIp=request.getRemoteAddr(); // 获取用户IP
			message.setUserIp(userIp);
			if(message.getId()==null){
				message.setId(DateUtil.getCurrentDateStr());
				resultTotal=messageService.add(message);
				// 该评论或该回复的点赞次数加1
				/*Blog blog=blogService.findById(comment.getBlog().getId());
				blog.setReplyHit(blog.getReplyHit()+1);
				blogService.update(blog);*/
			}
			if(resultTotal>0){
				result.put("success", true);
			}else{
				result.put("success", false);
			}
			if(StringUtils.isBlank(sNickName) || !nickName.equals(sNickName)) {
				session.setAttribute("nickName", nickName);
				session.setAttribute("email", email);
			}
		}
		ResponseUtil.write(response, result);
		return null;
	}
	
	/**
	 * 留言点赞
	 * @param comment
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/addStars")
	public String addStars(@RequestParam(value="id")String id,@RequestParam(value="stars")String stars,HttpServletResponse response)throws Exception{
		Message comment=new Message();
		comment.setStars(stars);
		comment.setId(id);
		messageService.update(comment);
		JSONObject result=new JSONObject();
		result.put("success", true);
		ResponseUtil.write(response, result);
		return null;
	}

}
